<?php

//phpinfo();
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//$con = pg_connect('host=192.168.43.5 dbname=monografia user=postgres');

function hash2()
{
    $CaracteresAceitos = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $max = strlen($CaracteresAceitos) - 1;
    $password = null;
    for ($i = 0; $i < 6; $i++) {
        $password .= $CaracteresAceitos{mt_rand(0, $max)};
    }
    return $password;
}

for ($i = 0; $i < 1000; $i++) {
    echo "'";
    echo hash2();
    echo "',<br />";
}
die;
$file = fopen('../top-1m.csv', 'r');
$i = 0;
while ($i < 1000000) {
    $a = fgetcsv($file);
    $site = 'www.' . $a[1];
    //
    //$hash = substr(md5($site), 0, 3) . substr(sha1($site), 0, 3);
    $hash = hash2();
    $res = pg_query_params($con, 'INSERT INTO url(hash, url, datacriacao)
                            VALUES ($1, $2, current_timestamp)', Array($hash, $site));

    //break;
    if ($res) {
        $i++;
    }
    echo $i . PHP_EOL;
}